package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import redis.clients.jedis.JedisPool;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐服务
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private FreeMarkerConfig freeMarkerConfig;
    @Value("${out_put_path}")
    private String outPutPath;//从属性文件中读取要生成的html对应的目录

    // 新增套餐数据
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        //获取id
        Integer setmealId = setmeal.getId();
        //添加关联关系
        this.setcheckgroupandsetmeal(setmealId, checkgroupIds);
        //将图片保存到redis中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);
        //用来生成静态页面(套餐列表，每一个套餐详情)
        generateMobileStaticHtml();
    }

    //用来生成静态页面(套餐列表，每一个套餐详情)
    public void generateMobileStaticHtml() {
        //调用dao查询套餐数据
        List<Setmeal> setmealList = setmealDao.getAllSetmeal();
        //生成套餐列表静态页面
        generateMobileSetmealListHtml(setmealList);
        //生成套餐详情静态页面(多个)
        generateMobileSetmealDetailHtml(setmealList);

    }

    //生成套餐列表静态页面
    public void generateMobileSetmealListHtml(List<Setmeal> setmealList) {
        Map map = new HashMap<>();
        map.put("setmealList", setmealList);
        generateHtml("mobile_setmeal.ftl", "m_setmeal.html", map);
    }

    //生成套餐详情静态页面(多个)
    public void generateMobileSetmealDetailHtml(List<Setmeal> setmealList) {
            for (Setmeal setmeal : setmealList) {
                Map setMealmap = new HashMap<>();
                setMealmap.put("setmeal", this.findById(setmeal.getId()));
                generateHtml("mobile_setmeal_detail.ftl", "setmeal_detail_" + setmeal.getId() + ".html", setMealmap);

            }
        }


    //生成静态页面
    public void generateHtml(String templateName, String HtmlName, Map map) {
        Configuration configuration = freeMarkerConfig.getConfiguration();
        // 创建Writer对象
        Writer out = null;
        try {
            //模板名字
            Template template = configuration.getTemplate(templateName);
            out = new FileWriter(new File(outPutPath + "/" + HtmlName));
            //输出
            template.process(map, out);
            //关闭Writer对象
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //分页查
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //        mybatis 分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    //查询套餐数据
    @Override
    public List<Setmeal> getAllSetmeal() {
        List<Setmeal> setmealList = setmealDao.getAllSetmeal();
        return setmealList;
    }

    //查询具体的每一个体检套餐的详情
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        return setmeal;
    }
    //查询套餐预约占比的数据
    @Override
    public List<Map<String,Object>> findByName() {
        List<Map<String,Object>> list= setmealDao.findByNameAndID();
        return list;
    }

    @Override
    public List<String> findAllSetmeal() {
        return setmealDao.findAllSetmeal();
    }

    //    抽取
    public void setcheckgroupandsetmeal(Integer setmealId, Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmeal_id", setmealId);
                map.put("checkgroup_id", checkgroupId);
                setmealDao.setcheckgroupandsetmeal(map);
            }
        }
    }
}
